package com.ctsi.offlinesupport.service;


import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.offlinesupport.dto.CustomtjDTO;
import com.ctsi.security.ISecurity;
import com.ctsi.supportflow.app.entity.ProjectEntity;
import com.ctsi.supportflow.app.vo.AppCustomCityVO;
import com.ctsi.supportflow.app.vo.AppCustomVO;
import com.ctsi.supportflow.app.vo.AppProjectVO;
import com.ctsi.supportflow.app.vo.ProjectHyVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class ProjectEntityService {
	private static final Logger logger=LoggerFactory.getLogger(ProjectEntityService.class);
	@Autowired
	private MybatisGenericDao dao;
	@Autowired
	private ISecurity iSecurity;
	/**
	 * 首页驾驶舱项目售前中后统计
	 */
	public Map<String,Object> selectLeaderProjectTj(){
		List<CustomtjDTO> o = (List<CustomtjDTO>) dao.queryForList(ProjectEntity.class.getName() + ".selectLeaderProjectTj");
		Map<String,Object> map=new HashMap<>();
		for (CustomtjDTO customtjDTO :o){
			map.put(customtjDTO.getKey1(), customtjDTO.getNum());
		}
		return map;
	}
	/**
	 * 添加
	 */
	public void insertProjectEntity(ProjectEntity projectEntity){
		if(UtilValidate.isEmpty(projectEntity.getId()))
			projectEntity.setId(UUIDGenerator.getId());
		logger.debug("insert {}",projectEntity);
		dao.insert(projectEntity);
	}
	/**
	 * 动态更新
	 */
	public int updateProjectEntityByPKSelective(ProjectEntity projectEntity){
		logger.debug("updateByPrimaryKeySelective {}",projectEntity);
		return dao.update(ProjectEntity.class.getName()+".updateByPrimaryKeySelective", projectEntity);
	}
	/**
	 * 更新
	 */
	public int updateProjectEntity(ProjectEntity projectEntity){
		logger.debug("update {}",projectEntity);
		return dao.update(projectEntity);
	}


	public ProjectEntity editProjectEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (ProjectEntity)dao.queryForObject(ProjectEntity.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 ProjectEntity对象
	 */
	public ProjectEntity findProjectEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(ProjectEntity.class, id);
	}
	/**
	 * 删除ProjectEntity对象
	 */
	public int removeProjectEntity(String id){
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(ProjectEntity.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}



	/**
	 * 详细页查询
	 */
	public ProjectEntity findByDetail(String id){
		return (ProjectEntity)dao.queryForObject(ProjectEntity.class.getName()+".findByDetail",id);
	}
	/**
	 * 多条件查询结果
	 */
	public Page<ProjectEntity> findByMulitCondition(Map<String,Object> param ,String userLoginId,String userId, int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
		{
			if (!iSecurity.isControl(userLoginId,"organChain","SELECT")) {
				return dao.queryPage(ProjectEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
			}else {
				Set<String> dataObjectIds = iSecurity.findDataObjectIds(userLoginId,"organChain","SELECT");
				if (UtilValidate.isNotEmpty(dataObjectIds)) {
					param.put("dataObjectIds",dataObjectIds);
				} else {
					param.put("createBy",userId);
				}
				return dao.queryPage(ProjectEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
			}

		}
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/**
	 * 多条件查询结果不分页
	 */
	public List<ProjectEntity> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return (List<ProjectEntity>)dao.queryForList(ProjectEntity.class.getName()+".selectByMap",map);
	}

	/**
	 * 根据客户ID查找项目
	 * @param coustomId
	 * @return
	 */
	public List<ProjectEntity> findProjectByCoustomId(String coustomId){
		if (UtilValidate.isNotEmpty(coustomId)) {
			Map<String,Object> params = new HashMap<>(1);
			params.put("customId",coustomId);
			return (List<ProjectEntity>) dao.queryForList(ProjectEntity.class.getName()+".selectMulitCondition",params);
		}
		return Collections.emptyList();
	}


	/**
	 * APP 项目页面
	 */
	public AppProjectVO findProjectHyVO(Map<String,Object> params, String userLoginId){
		AppProjectVO vo = new AppProjectVO();
		boolean isContrl = iSecurity.isControl(userLoginId,"organChain","SELECT");
		if (!isContrl) {
			vo.setProjectHys((List<ProjectHyVO>) dao.queryForList(ProjectEntity.class.getName()+".queryProjectHyNum",params));
			vo.setAllCount((Long) dao.queryForObject(ProjectEntity.class.getName()+".queryAppProjectHyNum",params));
			return vo;
		} else {
			Set<String> dataObjectIds = iSecurity.findDataObjectIds(userLoginId,"organChain","SELECT");
			if (UtilValidate.isEmpty(dataObjectIds)) {
				dataObjectIds.add("nodata");
			}
			params.put("dataObjectIds",dataObjectIds);
			vo.setProjectHys((List<ProjectHyVO>) dao.queryForList(ProjectEntity.class.getName()+".queryProjectHyNum",params));
			vo.setAllCount((Long) dao.queryForObject(ProjectEntity.class.getName()+".queryAppProjectHyNum",params));
			return vo;
		}
	}

	/**
	 * app 客户页面
	 * @return
	 */
	public AppCustomVO findAppCustom(Map<String,Object> params,String userLoginId){
		boolean isContrl = iSecurity.isControl(userLoginId,"organChain","SELECT");
		if (!isContrl) {
			long count  = (long) dao.queryForObject(ProjectEntity.class.getName()+".queryCustomApp",params);
			List<AppCustomCityVO> list = (List<AppCustomCityVO>) dao.queryForList(ProjectEntity.class.getName()+".queryCustomCity",params);
			AppCustomVO vo = new AppCustomVO();
			vo.setAllCount(count);
			vo.setCustomCity(list);
			return vo;
		} else {
			Set<String> dataObjectIds = iSecurity.findDataObjectIds(userLoginId,"organChain","SELECT");
			if (UtilValidate.isEmpty(dataObjectIds)) {
				return new AppCustomVO();
			} else {
				params.put("dataObjectIds",dataObjectIds);
				long count  = (long) dao.queryForObject(ProjectEntity.class.getName()+".queryCustomApp",params);
				List<AppCustomCityVO> list = (List<AppCustomCityVO>) dao.queryForList(ProjectEntity.class.getName()+".queryCustomCity", params);
				AppCustomVO vo = new AppCustomVO();
				vo.setAllCount(count);
				vo.setCustomCity(list);
				return vo;
			}
		}
	}

}
